package com.woniu108.person.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu108.carService.commos.dto.UserInfoDto;
import com.woniu108.carService.commos.dto.UserSimpleDto;
import com.woniu108.carService.commos.exception.WoniuException;
import com.woniu108.person.mapper.UserInfoMapper;
import com.woniu108.person.model.UserInfo;
import com.woniu108.person.param.UserListParam;
import com.woniu108.person.param.UserLoginParam;
import com.woniu108.person.service.UserInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;
    @Override
    public Page<UserInfoDto> allUserList(UserListParam param) {
        //根据筛选条件获取userInfo集合
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<UserInfo>();
        String userPhone = param.getUserPhone();
        String userNickName = param.getUserNickName();
        String userRealName = param.getUserRealName();
        Integer userState = param.getUserState();
        if(userPhone!=null) wrapper.like("user_phone",userPhone);
        if(userNickName!=null) wrapper.like("user_nick_name",userNickName);
        if(userRealName!=null) wrapper.like("user_real_name",userRealName);
        if(userState!=null) wrapper.eq("user_state",userState);
        List<UserInfo> infoList = userInfoMapper.selectList(wrapper);
        //转为userInfoDto集合同时分页
        long total = infoList.size();
        long pages=total%param.getPageSize()==0?total/param.getPageSize():total/param.getPageSize()+1;
        Page<UserInfoDto> page=new Page<UserInfoDto>();
        ArrayList<UserInfoDto> dtos=new ArrayList<>();
        int i=0;
        for (UserInfo userInfo : infoList) {
            if(i<param.getPageNum()*param.getPageSize().intValue()&&i>=(param.getPageNum()-1)*param.getPageSize().intValue()){
                UserInfoDto dto = BeanUtil.toBean(userInfo, UserInfoDto.class);
                dtos.add(dto);
            }
            i=i+1;
        }
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());
        page.setRecords(dtos);
        page.setTotal(total);
        page.setPages(pages);
        return page;
    }

    @Override
    public UserSimpleDto userCodeLogin(String userPhone) {
        //根据用户手机号获取用户信息判断状态
        QueryWrapper<UserInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("user_phone",userPhone);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);
        if(ObjectUtil.isEmpty(userInfo)) throw new WoniuException("用户不存在",10001);
        //userInfo转UserSimpleDto返回
        UserSimpleDto dto = BeanUtil.toBean(userInfo, UserSimpleDto.class);
        return dto;
    }

    @Override
    public UserSimpleDto userLogin(UserLoginParam param) {
        //根据用户手机号获取用户信息判断状态
        QueryWrapper<UserInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("user_phone",param.getUserPhone());
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);
        if(ObjectUtil.isEmpty(userInfo)) throw new WoniuException("用户不存在",10001);
        //判断密码是否正确
        if(!userInfo.getUserPass().equals(param.getUserPass())) throw new WoniuException("密码有误",10001);
        //userInfo转UserSimpleDto返回
        UserSimpleDto dto = BeanUtil.toBean(userInfo, UserSimpleDto.class);
        return dto;
    }
}
